<template>
  <div style="max-height: 700px">
    <!-- 面包屑导航条 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>权限列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!--列表-->
      <el-form :inline="true" class="toolbar">
        <el-form-item>
          <el-button
            @click="addRoot"
            type="success"
            style="padding: 8px 10px; font-size: 12px"
            >添加根节点</el-button
          >
        </el-form-item>
      </el-form>
      <el-tree
        :data="treeData"
        :props="defaultProps"
        node-key="id"
        v-loading="treeLoading"
        default-expand-all
        :expand-on-click-node="false"
        ref="tree"
      >
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <span>{{ data.name }}</span>
          <span>
            <el-button
              :disabled="node.level == 1 ? false : true"
              style="padding: 5px 8px; z-index: 9999"
              type="success"
              size="mini"
              @click="() => addNode(data)"
              >{{ getdis(node, data) }}添加子权限</el-button
            >
            <el-button
              size="mini"
              style="padding: 5px 8px; z-index: 9999"
              @click="() => edit(data)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="danger"
              style="padding: 5px 8px; z-index: 9999"
              @click="() => deletePermission(data)"
              >删除</el-button
            >
            <!-- <el-button
                    type="text"
                    size="mini"
                    @click="() => remove(node, data)">
                    Delete
                </el-button> -->
            <!-- <el-button style="padding: 5px 8px;" :disabled="data.children&&data.children.length>0 ? true : false " type="danger" size = "mini"  @click="() => del(data)" >删除</el-button> -->
          </span>
        </span>
      </el-tree>
    </el-card>
    <!--添加-->
    <el-dialog
      title="添加权限"
      :visible.sync="addDialogVisible"
      :close-on-click-modal="false"
      size="tiny"
    >
      <el-form
        :model="addForm"
        label-position="left"
        label-width="80px"
        :rules="inputRule"
        ref="addForm"
      >
        <el-form-item label="id" prop="id">
          <el-input
            v-model="addForm.id"
            auto-complete="off"
            disabled="disabled"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="父级id" prop="parentId">
          <el-input v-model="addForm.parentId" auto-complete="off"></el-input>
        </el-form-item> -->
        <el-form-item label="名称" prop="name">
          <el-input v-model="addForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="路径" prop="rule">
          <el-input v-model="addForm.rule" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="addFormSubmit"
          :loading="addFormLoading"
          >提 交</el-button
        >
      </div>
    </el-dialog>
    <!--修改-->
    <el-dialog
      title="修改权限"
      :visible.sync="editDialogVisible"
      :close-on-click-modal="false"
      size="tiny"
    >
      <el-form
        :model="editForm"
        label-position="left"
        label-width="80px"
        :rules="inputRule"
        ref="editForm"
      >
        <el-form-item label="id" prop="permissionCode">
          <el-input v-model="editForm.id" auto-complete="off"></el-input>
        </el-form-item>
        <!-- <el-form-item label="父级id" prop="parentId">
          <el-input v-model="editForm.parentId" auto-complete="off"></el-input>
        </el-form-item> -->
        <el-form-item label="名称" prop="name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="路径" prop="rule">
          <el-input v-model="editForm.rule" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="路径" prop="rule">
          <el-input v-model="editForm.rule" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="editFormSubmit"
          :loading="editFormLoading"
          >提 交</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  inject:['reload'],
  data() {
    
    return {
      addDialogVisible: false,
      editDialogVisible: false,
      addForm: {
        id: "",
        name: "",
        parentId: "",
        rule: "",
      },
      editForm: {
        id: "",
        name: "",
        parentId: "",
        rule: "",
      },
      treeData: [
        {
          name: "配置管理",
          path: "/config",
          fatherId: 0,

          children: [
            {
              name: "七牛配置",
              path: "/config/list",
              fatherId: 1,
            },
          ],
        },
      ],
    };
  },
  mounted() {
    this.getRightsList();
  },
  methods: {
    deletePermission(data) {
      console.log(data);
      this.$confirm('确认删除该项列表权限？', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http
            .post("/manage/admin/deleteRule", { queryId: data.id })
            .then((res) => {
              // console.log(res)
              if (res.data.status == 1) {
                this.editDialogVisible = false;
                this.getRightsList();
                this.$message({
                  message: "删除成功",
                  type: "success",
                });
                this.reload();
              } else {
                this.$message.error(res.data.message);
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    addRoot() {
      this.addDialogVisible = true;
      // this.$refs.addForm && this.$refs.addForm.resetFields()
      // this.addForm.id = '0'
      this.addForm.parentId = "0";
    },
    addNode(parent) {
      console.log(parent);
      this.addDialogVisible = true;
      // this.$refs.addForm && this.$refs.addForm.resetFields();
      this.addForm.parentId = parent.id;
    },
    remove() {
      console.log(2);
    },
    edit(row) {
      console.log(row);
      // alert(1)
      // console.log(node)
      // console.log(data)
      this.editDialogVisible = true;
      this.editDialogVisible = true;
      this.editForm.id = row.id;
      this.editForm.name = row.name;
      this.editForm.parentId = row.fatherId;
      this.editForm.rule = row.path;
    },
    getdis(node, data) {
      // console.log(node)
      // console.log(data)
    },
    editFormSubmit() {
      this.$http
        .post("/manage/admin/addOrEditRules", this.editForm)
        .then((res) => {
          // console.log(res)
          if (res.data.status == 1) {
            this.editDialogVisible = false;
            this.getRightsList();
            this.$message({
              message: "修改成功",
              type: "success",
            });
            this.reload();
          } else {
            this.$message.error(res.data.message);
          }
        });
    },
    addFormSubmit() {
      this.$http
        .post("/manage/admin/addOrEditRules", this.addForm)
        .then((res) => {
          // console.log(res)
          if (res.data.status == 1) {
            this.addDialogVisible = false;
            this.getRightsList();
            this.$message({
              message: "添加成功",
              type: "success",
            });
            this.reload();
          } else {
            this.$message.error(res.data.message);
          }
        });
    },
    getRightsList() {
      this.$http.post("/manage/admin/getAllAdminRules").then((res) => {
        // console.log(res)
        if (res.data.status == 1) {
          this.treeData = res.data.data;
        }
      });
    },
    renderContent(h, { node, data, store }) {
      // console.log(node)
      //  return (
      //       <span class="custom-tree-node" style="flex: 1;"><span><span> </span></span><span style = "float: right; margin-right: 20px">
      //   </span></span>
      //    <el-button  type="success" size = "mini"  on-click = {() => this.addNode(data)} >添加子权限</el-button>
      //   <el-button size = "mini" on-click = {() => this.edit(data)} >修改</el-button>
      //   <el-button style="display:none;"  disabled={data.children&&data.children.length>0} type="danger" size = "mini"  on-click = {() => this.del(data)} >删除</el-button>
      //   <span><span><span> {data.permissionName}({data.permissionUrl}) </span></span><span style = "float: right; margin-right: 20px">
      //   <el-button disabled={node.level!=1} type="success" size = "mini"  on-click = {() => this.addNode(data)} >添加子权限</el-button>
      //   <el-button size = "mini" on-click = {() => this.edit(data)} >修改</el-button>
      //   <el-button style="display:none;"  disabled={data.children&&data.children.length>0} type="danger" size = "mini"  on-click = {() => this.del(data)} >删除</el-button>
      //   </span></span>
      // )
    },
  },
};
</script>
<style>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  /* padding: 20px 10px; */
}
</style>
